package servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class BServlet extends HttpServlet {
    @Override
    public void init(ServletConfig config) throws ServletException {

    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        //false 返回当前session，没有返回true
        //true  返回当前session，没有先创建后返回
        HttpSession session = req.getSession(true);

        //设置失效时间 单位秒 web.xml也可以设置
        session.setMaxInactiveInterval(2000);

        System.out.println("id " + session.getId());
        System.out.println("is New " + session.isNew());
        System.out.println("最大时效 " + session.getMaxInactiveInterval());
        System.out.println("创建时间 " + session.getCreationTime());
        System.out.println("上次访问时间 " + session.getLastAccessedTime());

        //session.invalidate();

        //浏览器禁用了cookie时有效，使用url传递sessionid
        String encodeURL = resp.encodeURL("aservlet");
        System.out.println("encode url " + encodeURL);
    }

    @Override
    public void destroy() {
        System.out.println("bservlet destroy");
    }

}



